//===应用层
#include "AppLayer.h"

#include "key.h"
#include "xm_key.h"
#include "spi.h"
#include "uart.h"
#include "WS2812_Control.h"


XCLed_Str xcled_str;

void System_Init(void)
{
    xcled_str.power_on = SYSTEM_BOOT_DOWN;
    xcled_str.mode = 0;
    xcled_str._H = 0;
    xcled_str._S = 255;
    xcled_str._V = 128;
}


// 关机状态 切换到 开机状态
void ShutDown_Jump_BootUp(void)
{
    if(xcled_str.power_on == EFFECT_OFF)
    {
        xcled_str.power_on = EFFECT_ON;
        powerOn();
    }
    
}

void BootUp_Jump_ShutDown(void) // 开机状态 切换到 关机状态
{
    if(xcled_str.power_on == EFFECT_ON)
    {
        xcled_str.power_on = EFFECT_OFF;
        powerOff();
    }
    
}

unsigned char u8TouchKey_Map[] =
{
    KEY_MODE,
    KEY_H,
    KEY_V,
    // 组合按键
    KEY_R,
};

#define DEBUG_PRINT_KEY 1

static unsigned char Key_Scan(void)
{
    unsigned char key_value = 0;
#if DEBUG_PRINT_KEY
    static unsigned char key_value_pre = 0;
#endif
    unsigned char u8Touch_Map = 0;
    unsigned char u8Touch_Press_Number = 0;
    unsigned char u8Touch_Value = 0;
    unsigned char u8Touch_Shift = 0;
    unsigned char i = 0;

    if (HAL_GPIO_ReadPin(M_PORT, M_PIN) == GPIO_PIN_RESET)
    {
        u8Touch_Map |= 1;
    }
    else
    {
        u8Touch_Map &= ~1;
    }
    if (HAL_GPIO_ReadPin(H_PORT, H_PIN) == GPIO_PIN_RESET)
    {
        u8Touch_Map |= 2;
    }
    else
    {
        u8Touch_Map &= ~2;
    }
    if (HAL_GPIO_ReadPin(V_PORT, V_PIN) == GPIO_PIN_RESET)
    {
        u8Touch_Map |= 8;
    }
    else
    {
        u8Touch_Map &= ~8;
    }
#if 0
    printf("u8Touch_Map = %d\n", u8Touch_Map);
#endif

    while (u8Touch_Map != 0)
	{
		if (u8Touch_Map & 0x01)
		{
			u8Touch_Press_Number++;
			if (u8Touch_Press_Number == 1)
				u8Touch_Value = i;
			else if (u8Touch_Press_Number == 2)
				u8Touch_Shift = i;
			else
				break;
		}
		i++;
		u8Touch_Map >>= 1;
	}
    switch (u8Touch_Press_Number)
    {
        case 1:
            key_value = u8TouchKey_Map[u8Touch_Value];
            break;
        case 2:
        {
            key_value = 0xFF;
            // u8Touch_Last_Number = 2;
            u8Touch_Value = u8TouchKey_Map[u8Touch_Value];
            u8Touch_Shift = u8TouchKey_Map[u8Touch_Shift];
            // 组合按键处理
            switch (u8Touch_Value)
            {
                case KEY_H:
                {
                    if (u8Touch_Shift == KEY_V)
                    {
                        key_value = KEY_R;
                    }
                    break;
                }

                case KEY_V:
                {
                    if (u8Touch_Shift == KEY_H)
                    {
                        key_value = KEY_R;
                    }
                    break;
                }

                
            
            default:
                break;
            }

            break;
        }
           
        default:
            break;
    }

#if DEBUG_PRINT_KEY
    //按键调试
    if(key_value != key_value_pre)
    {
        printf("key_value = %d\r\n", key_value);
        key_value_pre = key_value;
    }
#endif 

    return key_value;
}


//===50MS按键解析
void Key_Analysis_50MS(void)
{
    unsigned char u8Key_Value = 0;
    unsigned char i = 0;

    u8Key_Value = Key_Scan();

    for (i = 0; i < KEY_NUM; i++)
		LongKeyEvents(&stLongKey_Handle[i], u8Key_Value);
}

//===50MS软件定时器
void SoftwareTimer_XCScan_5ms(void)
{
    updateEffects();
    updateEffects_slow();
    // HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_2);
}

extern uint8_t effectHue;
//===1000MS软件定时器
void SoftwareTimer_1000MS(void)
{
    printf("powerState = %d\n",powerState);
    printf("currentEffect = %d\n",effectHue);
    printf("effectHue = %d\n",effectHue);
    // printf("effectHue = %d\n",effectHue);

}

